<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form>
        <el-form-item>
          <el-button type="primary" icon="plus" v-if="hasPerm('user:add')" @click="showCreate">添加
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="拼命加载中" border fit
              highlight-current-row>
      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)"> </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="昵称" prop="nickname" style="width: 60px;"></el-table-column>
      <el-table-column align="center" label="用户名" prop="username" style="width: 60px;"></el-table-column>
      <el-table-column align="center" label="角色" width="100">
        <template slot-scope="scope">
          <el-tag type="success" v-text="scope.row.roleName" v-if="scope.row.roleId===1"></el-tag>
          <el-tag type="primary" v-text="scope.row.roleName" v-else></el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime" width="170"></el-table-column>
      <el-table-column align="center" label="最近修改时间" prop="updateTime" width="170"></el-table-column>
      <el-table-column align="center" label="管理" width="220" v-if="hasPerm('user:update')">
        <template slot-scope="scope">
          <el-button type="primary" icon="edit" @click="showUpdate(scope.$index)" size="small">修改</el-button>
          <!-- <el-button type="success"  size="small" @click="open(scope.$index)">详情</el-button> -->
          <!-- <el-button type="primary" icon="edit" @click="showUpdate(scope.$index)">详情</el-button> -->
          <el-button type="danger" icon="delete" size="small" v-if="scope.row.userId!=userId "
                     @click="removeUser(scope.$index)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-size="listQuery.pageRow"
      :total="totalCount"
      :page-sizes="10"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form class="small-space" :model="tempUser" label-position="left" label-width="80px"
               style='width: 300px; margin-left:50px;'>
        <el-form-item label="用户名" required v-if="dialogStatus=='create'">
          <el-input type="text" v-model="tempUser.username">
          </el-input>
        </el-form-item>
        <el-form-item label="密码" v-if="dialogStatus=='create'" required>
          <el-input type="password" v-model="tempUser.password">
          </el-input>
        </el-form-item>
        <el-form-item label="新密码" v-else>
          <el-input type="password" v-model="tempUser.password" placeholder="不填则表示不修改">
          </el-input>
        </el-form-item>
        <el-form-item label="角色" required>
          <el-select v-model="tempUser.roleId" placeholder="请选择">
            <el-option
              v-for="item in roles"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="昵称" required>
          <el-input type="text" v-model="tempUser.nickname">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus=='create'" type="success" @click="createUser">创 建</el-button>
        <el-button type="primary" v-else @click="updateUser">修 改</el-button>
      </div>
    </el-dialog>
    <el-dialog  :visible.sync="detailsShow">
      <p>作者姓名：<span v-text="this.authorInfo.nickname"></span></p>
      <p>ID：<span v-text="this.authorInfo.username"></span></p>
      <p>电话：<span v-text="this.authorInfo.phoneNum"></span></p>
      <p>微信：<span v-text="this.authorInfo.wechatId"></span></p>
      <p>邮箱：<span v-text="this.authorInfo.mailbox"></span></p>
      <h6>作者文章统计</h6>
      <p>发布的文章共计：<span v-text="this.authorInfo.basiCount"></span> 篇</p>
      <p>发布：<span>100</span> 篇</p>
      <p>驳回：<span v-text="this.authorInfo.rejectCount">500</span> 篇</p>
      <p>修改：<span>399</span> 篇</p>
    </el-dialog>
    <!-- 用户详情展示列表 -->
  </div>
</template>
<script>
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      totalCount: 0, //分页组件--数据总条数
      list: [], //表格的数据
      listLoading: false, //数据加载等待动画
      listQuery: {
        pageNum: 1, //页码
        pageRow: 10 //每页条数
      },
      roles: [], //角色列表
      dialogStatus: "create",
      dialogFormVisible: false,
      detailsShow: false,
      textMap: {
        update: "编辑",
        create: "新建用户"
      },
      tempUser: {
        username: "",
        password: "",
        nickname: "",
        roleId: "",
        userId: ""
      },
      authorInfo:[],
    };
  },
  created() {
    this.getList();
    if (this.hasPerm("user:add") || this.hasPerm("user:update")) {
      this.getAllRoles();
    }
    this.authorArticleFun();
  },
  computed: {
    ...mapGetters(["userId"])
  },
  methods: {
    getAllRoles() {
      this.api({
        url: "/user/getAllRoles",
        method: "get"
      }).then(data => {
        this.roles = data.list;
      });
    },
    getList() {
      //查询列表
      this.listLoading = true;
      this.api({
        url: "/user/list",
        method: "get",
        params: this.listQuery
      }).then(data => {
        this.listLoading = false;
        this.list = data.list;
        this.totalCount = data.totalCount;
      });
    },
    handleSizeChange(val) {
      //改变每页数量
      this.listQuery.pageRow = val;
      this.handleFilter();
    },
    handleCurrentChange(val) {
      //改变页码
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleFilter() {
      //查询事件
      this.listQuery.pageNum = 1;
      this.getList();
    },
    getIndex($index) {
      //表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1;
    },
    showCreate() {
      //显示新增对话框
      this.tempUser.username = "";
      this.tempUser.password = "";
      this.tempUser.nickname = "";
      this.tempUser.roleId = "";
      this.tempUser.userId = "";
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    showUpdate($index) {
      let user = this.list[$index];
      this.tempUser = user;
      this.tempUser.deleteStatus = "1";
      this.tempUser.password = "";
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
    },
    createUser() {
      //添加新用户
      console.log(this.tempUser);
      this.api({
        url: "/user/add",
        method: "post",
        data: this.tempUser
      }).then(() => {
        this.getList();
        this.dialogFormVisible = false;
      });
    },
    updateUser() {
      //修改用户信息
      let _vue = this;
      this.api({
        url: "/user/update",
        method: "post",
        data: this.tempUser
      }).then(() => {
        let msg = "修改成功";
        this.dialogFormVisible = false;
        if (this.userId === this.tempUser.userId) {
          msg = "修改成功,部分信息重新登录后生效";
        }
        this.$message({
          message: msg,
          type: "success",
          duration: 1 * 1000,
          onClose: () => {
            _vue.getList();
          }
        });
      });
    },
    removeUser($index) {
      let _vue = this;
      this.$confirm("确定删除此用户?", "提示", {
        confirmButtonText: "确定",
        showCancelButton: false,
        type: "warning"
      }).then(() => {
        let user = _vue.list[$index];
        user.deleteStatus = "2";
        _vue
          .api({
            url: "/user/delete",
            method: "post",
            data: user
          })
          .then(() => {
            _vue.getList();
          })
          .catch(() => {
            _vue.$message.error("删除失败");
          });
      });
    },
    //作者文章
    authorArticleFun() {
      this.api({
        url: "/article/statistics",
        method: "get"
      }).then(data => {
        console.log(data);
      });
    },
    //用户详情显示
    open(index) {
      this.detailsShow = true;
      this.api({
        url: "/article/authorInfo",
        method: "post",
        data: {
          userId: this.list[index].userId
        }
      }).then(data => {
        this.authorInfo = data
        console.log(data)
        // this.$alert(
        //   "<p>作者姓名：<span>" +
        //     data.nickname +
        //     "</span></p><p>ID：<span>" +
        //     data.username +
        //     "</span></p><p>电话：<span>" +
        //     data.phoneNum +
        //     "</span></p><p>微信：<span>" +
        //     data.wechatId +
        //     "</span></p><p>邮箱：<span>" +
        //     data.mailbox +
        //     "</span></p><h6>作者文章统计</h6><p>发布的文章共计：999篇</p><p>发布：100篇</p><p>驳回：500篇</p><p>修改：399篇</p>",
        //   "作者信息",
        //   {
        //     confirmButtonText: "确定",
        //     dangerouslyUseHTMLString: true
        //   }
        // );
      });
    }
  }
};
</script>
